/**
 * 配置
 * */
package org.zmhhxl.sample3.oauth2.a.authentications.strategy.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Component;
import org.zmhhxl.sample3.oauth2.a.authentications.model.CustomOAuth2User;
import org.zmhhxl.sample3.oauth2.a.authentications.strategy.Oauth2UserConverterStrategy;

import static org.zmhhxl.sample3.oauth2.a.authentications.constant.AuthenticationConstant.THIRD_LOGIN_GITHUB;

/**
 * 转换通过Github登录的用户信息
 *
 * @author zmhhxl
 */
@RequiredArgsConstructor
@Component(THIRD_LOGIN_GITHUB)
public class GithubUserConverter implements Oauth2UserConverterStrategy {

   private final GiteeUserConverter userConverter;

   protected static final String LOGIN_TYPE = THIRD_LOGIN_GITHUB;

   @Override
   public CustomOAuth2User convert(OAuth2User oAuth2User) {
      // github与gitee目前所取字段一致，直接调用gitee的解析
      CustomOAuth2User basicOauth2User = userConverter.convert(oAuth2User);
      // 提取location
      Object location = oAuth2User.getAttributes().get("location");
      basicOauth2User.setLocation(location + "");
      // 设置登录类型
      basicOauth2User.setType(LOGIN_TYPE);
      return basicOauth2User;
   }
}
